<?php

/**
 * 物流需求列表
 * @author wyb
 */

namespace Logistics\Controller;
class NeedsController extends BaseController {

//我的抢购订单列表
    public function index() {
	
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
       
        $keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
        $where = '';

        $LogisticsNeeds = M('LogisticsNeeds');
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $LogisticsNeeds->where($where)->count();

        $list = $LogisticsNeeds
            ->where($where)
            ->limit($offset . ',' . $pagesize)
            ->select();
     
        $page = new \Think\Page($count, $pagesize);
		 
        $page = $page->show();
		
		$this->assign('count', $count);
        $this->assign('list', $list);
        $this->assign('page', $page);
		
        $this->display();
    }
	
	//立即抢单
	public function detail(){
		$id = I('get.id');
	
		if($_POST){
			$data = I('post.');
			$info = M('LogisticsNeeds')->where(array('id'=>$data['need_id'],'is_choice'=>0))->find();
			$this->assign('info',$info);
			if(empty($info)){
				 $this->error('该需求不存在或已经过期！');
			}	
		
			$orderInfo = M('LogisticsOrder')->where(array('need_id'=>$data['need_id'],'supply_id'=>$this->_member_info['id']))->find();
			if($orderInfo){
				 $this->error('你已经报过价了！');
			}
			$data['create_time'] = time();
			$data['order_sn'] = date('YmdHis') . rand(1000, 9999);
			$data['supply_id'] = $this->_member_info['id'];
			if($order_id =  M('LogisticsOrder')->add($data)){
                $this->success('报价提交成功', U('Order/index'));
            } else {
                $this->error('报价失败');
            }
			 
			
		}else{
			
			$info = M('LogisticsNeeds')->where(array('id'=>$id))->find();
			 
			if(empty($info)){
				 $this->error('该需求不存在！');
			}
			  //变为已经查看
			M('LogisticsNeeds')->data(array('is_read'=>1))->where(array('id'=>$id))->save();
			$Oinfo = M('LogisticsOrder')->where(array('need_id'=>$id,'supply_id'=>$this->_member_info['id']))->find();
			if($Oinfo){
				$info['is_true'] = 1;
			}
			$this->assign('info',$info);
			$list = M('LogisticsOrder')->alias('o')->join('LEFT JOIN zt_logistics_supply as s ON o.supply_id = s.id')->where(array('o.need_id'=>$id))->field('o.*,s.company_name')->select();
			$this->assign('list', $list);
			$this->display();
		}
		
	}
	
	public function newtask(){
			$count = M('LogisticsNeeds')->where(array('is_read'=>0))->count();
			$this->ajaxReturn($count);
		}

	
	
	

}
